package com.example.mysaat.handle_utils;

import android.util.Log;

import com.example.mysaat.MainActivity;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ParseXMLUtil{
    private XmlPullParserFactory xmlPullParserFactory = null;
    private List<String> datas=null;
    private void parseXMLData(int id){
    //定义一个局部的List，目的让每次调用刷掉旧值
    datas = new ArrayList<String>();
        try {
            XmlPullParser parser = MainActivity.context.getResources().getXml(id);
            //获取读取事件的类型
            int eventType = parser.getEventType();
            while (eventType!=XmlPullParser.END_DOCUMENT){
                String nodeName = parser.getName();
                switch(eventType){
                    case XmlPullParser.START_TAG:{
                        if("value".equals(nodeName)){
                            datas.add(parser.nextText());
                        }
                    break;
                    }
                    case XmlPullParser.END_TAG:{
                            if("key".equals(nodeName)){
                                Log.d("XMLParseUtil","value parse finished");
                            }
                        break;
                    }
                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public List<String> getParseData(int id) {
        parseXMLData(id);
        return datas;

    }
}
